//
//  WYRedPacketMessageHeadCell.m
//  Boss
//
//  Created by admin on 2018/6/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYRedPacketMessageHeadCell.h"

@interface WYRedPacketMessageHeadCell()
@property (strong, nonatomic) IBOutlet UIImageView *userImageView;
@property (strong, nonatomic) IBOutlet UILabel *userName;
@property (strong, nonatomic) IBOutlet UILabel *redPacketCount;
@property (strong, nonatomic) IBOutlet UIButton *followBtn;



@property (strong, nonatomic) IBOutlet UIButton *redPacketState;

@property (strong, nonatomic) IBOutlet UIImageView *addressImageView;

@property (strong, nonatomic) IBOutlet UIImageView *exclusiveImageView;

@end

@implementation WYRedPacketMessageHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.userImageView setCornerRadius:33.f];
    self.followBtn.hidden = YES;
    [self.userImageView setBorderWithWidth:kLineHeight *4 andColor:[UIColor hexStringToColor:@"#fffaf2" alpha:1.0]];
    // Initialization code
}
-(void)setType:(NSString *)type
{
    _type = type;
    self.followBtn.hidden = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setRedMessage:(WYRedPacketMessage *)redMessage
{
    _redMessage = redMessage;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:redMessage.grantUserPhoto] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
    self.userName.text = redMessage.grantUserName;
   
    if (redMessage.isReceived.integerValue) {
      //  self.redPacketCount.text = [NSString stringWithFormat:@"%.3f 元",redMessage.myReceiveAmount.floatValue/1000.0];
        
        NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%.3f 元",redMessage.myReceiveAmount.floatValue/1000.0]];
        NSString *lenth = @"元";
        NSRange searchRange = NSMakeRange(0, aString.length);
        NSRange range = [aString.string rangeOfString:lenth options:kNilOptions range:searchRange];
        self.redPacketCount.font = kFontSystemBIgger2(40);
        self.redPacketCount.textColor = [UIColor hexStringToColor:@"#222222" alpha:1.0];
        [self.redPacketState setTitle:@"已存入您的钱包,可用于派发红包哦~" forState:(UIControlStateNormal)];
        //if (range.location == NSNotFound) return;
        
        
        [aString addAttribute:NSForegroundColorAttributeName value:[UIColor hexStringToColor:@"#222222" alpha:1.0] range:range];
        [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18]range:range];
         self.redPacketCount.attributedText = aString;
        
    }else{
        if (redMessage.surplus.integerValue==0) {
            self.redPacketCount.text = @"红包已派完";
            self.redPacketCount.font = kFontSystemBIgger2(22);
            self.redPacketCount.textColor = [UIColor hexStringToColor:@"#aaaaaa" alpha:1.0];
            [self.redPacketState setTitle:@"做任务可赢取更多拆红包机会哦～,去做任务 >" forState:(UIControlStateNormal)];
        }else{
            
            NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%.3f 元",redMessage.myReceiveAmount.floatValue/1000.0]];
            NSString *lenth = @"元";
            NSRange searchRange = NSMakeRange(0, aString.length);
            NSRange range = [aString.string rangeOfString:lenth options:kNilOptions range:searchRange];
            self.redPacketCount.font = kFontSystemBIgger2(40);
            self.redPacketCount.textColor = [UIColor hexStringToColor:@"#222222" alpha:1.0];
            [self.redPacketState setTitle:@"已存入您的钱包,可用于派发红包哦~" forState:(UIControlStateNormal)];
            //if (range.location == NSNotFound) return;
            
            
            [aString addAttribute:NSForegroundColorAttributeName value:[UIColor hexStringToColor:@"#222222" alpha:1.0] range:range];
            [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18]range:range];
            self.redPacketCount.attributedText = aString;
            
            
            
            
            
            
            
            
            
            
        }
//        self.redPacketCount.text = @"红包已派完";
//        self.redPacketCount.font = kFontSystemBIgger2(22);
//        self.redPacketCount.textColor = [UIColor hexStringToColor:@"#aaaaaa" alpha:1.0];
//         [self.redPacketState setTitle:@"做任务可赢取更多拆红包机会哦～,去做任务 >" forState:(UIControlStateNormal)];
    }
    if (self.type.length) {
        if (!redMessage.isMy.integerValue) {
             [self.redPacketState setTitle:[NSString stringWithFormat:@"%ld人已领取",redMessage.number.integerValue-redMessage.surplus.integerValue] forState:(UIControlStateNormal)];
        }else{
            [self.redPacketState setTitle:[NSString stringWithFormat:@"总计发放%.0f元/%@个，%ld人已领取(30%%手续费)",redMessage.money.floatValue/1000.0,redMessage.number,redMessage.number.integerValue-redMessage.surplus.integerValue] forState:(UIControlStateNormal)];
        }
       
        
        
    }
    
    
    
    switch (redMessage.scope.intValue) {
        case 1:{
            self.addressImageView.image = [UIImage imageNamed:@"area_three"];
        }
            
            break;
        case 2:{
            self.addressImageView.image = [UIImage imageNamed:@"area_city"];
        }
            
            break;
        case 3:{
            self.addressImageView.image = [UIImage imageNamed:@"area_dis"];
        }
            
            break;
        case 4:{
            self.addressImageView.image = [UIImage imageNamed:@"area_country"];
        }
            
            break;
        case 5:{
            self.addressImageView.image = [UIImage imageNamed:@"area_country"];
        }
            
            break;
        default:
            break;
    }
    
    
    switch (redMessage.accurateType.intValue) {
        case 0:{
            self.exclusiveImageView.hidden = YES;
        }
            break;
        case 1:{
            self.exclusiveImageView.image = [UIImage imageNamed:@"exclusive_Handsome"];
        }
            break;
        case 2:{
            self.exclusiveImageView.image = [UIImage imageNamed:@"exclusive_Beauty"];
        }
            break;
        case 3:{
            self.exclusiveImageView.image = [UIImage imageNamed:@"exclusive_boss"];
        }
            break;
        default:
            break;
    }
    switch (redMessage.vipLevel.integerValue) {
        case 10:
            self.exclusiveImageView.hidden = NO;
            self.exclusiveImageView.image = [UIImage imageNamed:@"icon_silver_excl_bottom"];
            break;
        case 20:
            self.exclusiveImageView.hidden = NO;
            self.exclusiveImageView.image = [UIImage imageNamed:@"icon_gold_excl_bottom"];
            break;
        case 30:
            self.exclusiveImageView.hidden = NO;
            self.exclusiveImageView.image = [UIImage imageNamed:@"icon_diamonds_excl_bottom"];
            break;
        default:
            break;
    }
    
    WYUserMessage *userMessage = [WYStoreModule currentUserMessage];
    
    
    if (redMessage.vipLevel.integerValue>userMessage.vipId.integerValue) {
        self.redPacketState.hidden = NO;
        [self.redPacketState setTitle:@"去开通会员领取该红包 >" forState:(UIControlStateNormal)];
        self.redPacketCount.text = @"您还不满足领取该红包的条件";
        self.redPacketCount.font = kFontSystemBIgger2(22);
        self.redPacketCount.textColor = [UIColor hexStringToColor:@"#aaaaaa" alpha:1.0];
    }
    
    
    
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gotoPage)];
    
    [self.userImageView addGestureRecognizer:ges];
    
    

}
-(void)gotoPage
{
    if ([self.delegate respondsToSelector:@selector(gotoPageWithUserId:)]) {
        [self.delegate gotoPageWithUserId:self.redMessage.grantUserId];
    }
}



-(void)setAdvertisementMessage:(WYAdvertisementMessage *)advertisementMessage
{
    _advertisementMessage = advertisementMessage;
    self.followBtn.hidden = YES;
    self.userName.text = advertisementMessage.grantUserName;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:advertisementMessage.grantUserPhoto] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
    if (advertisementMessage.surplus.integerValue==0) {
        self.redPacketCount.text = @"红包已派完";
        self.redPacketCount.font = kFontSystemBIgger2(22);
        self.redPacketCount.textColor = [UIColor hexStringToColor:@"#aaaaaa" alpha:1.0];
        [self.redPacketState setTitle:@"做任务可赢取更多拆红包机会哦～,去做任务 >" forState:(UIControlStateNormal)];
    }
//     if (advertisementMessage.isReceiveFinished.integerValue||advertisementMessage.isReceived)
    if (advertisementMessage.isReceived.integerValue){
        self.redPacketCount.font = kFontSystemBIgger2(40);
        self.redPacketCount.textColor = [UIColor hexStringToColor:@"#222222" alpha:1.0];
        NSMutableAttributedString *aString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%.3f 元",advertisementMessage.myReceiveAmount.floatValue/1000.0]];
        NSString *lenth = @"元";
        NSRange searchRange = NSMakeRange(0, aString.length);
        NSRange range = [aString.string rangeOfString:lenth options:kNilOptions range:searchRange];
        if (range.location == NSNotFound) return;
        [aString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15]range:range];
        self.redPacketCount.attributedText = aString;
        [self.redPacketState setTitle:@"已存入您的钱包,可用于派发红包哦~" forState:(UIControlStateNormal)];
        
    }else{
        self.redPacketCount.text = @"红包已派完";
        self.redPacketCount.font = kFontSystemBIgger2(22);
        self.redPacketCount.textColor = [UIColor hexStringToColor:@"#aaaaaa" alpha:1.0];
        [self.redPacketState setTitle:@"做任务可赢取更多拆红包机会哦～,去做任务 >" forState:(UIControlStateNormal)];
    }
    
   
    
    
    switch (advertisementMessage.scope.intValue) {
        case 1:{
            self.addressImageView.image = [UIImage imageNamed:@"area_three"];
        }
            
            break;
        case 2:{
            self.addressImageView.image = [UIImage imageNamed:@"area_city"];
        }
            
            break;
        case 3:{
            self.addressImageView.image = [UIImage imageNamed:@"area_dis"];
        }
            
            break;
        case 4:{
            self.addressImageView.image = [UIImage imageNamed:@"area_country"];
        }
            
            break;
        case 5:{
            self.addressImageView.image = [UIImage imageNamed:@"area_country"];
        }
            
            break;
        default:
            break;
    }
    
    
    switch (advertisementMessage.accurateType.intValue) {
        case 0:{
            self.exclusiveImageView.hidden = YES;
        }
            break;
        case 1:{
            self.exclusiveImageView.image = [UIImage imageNamed:@"exclusive_Handsome"];
        }
            break;
        case 2:{
            self.exclusiveImageView.image = [UIImage imageNamed:@"exclusive_Beauty"];
        }
            break;
        case 3:{
            self.exclusiveImageView.image = [UIImage imageNamed:@"exclusive_boss"];
        }
            break;
        default:
            break;
    }
     self.exclusiveImageView.hidden = NO;
    self.exclusiveImageView.image = [UIImage imageNamed:@"平台发布_标签"];
    
    
    if (self.type.length) {
         [self.redPacketState setTitle:[NSString stringWithFormat:@"%ld人已领取",advertisementMessage.number.integerValue-advertisementMessage.surplus.integerValue] forState:(UIControlStateNormal)];
        
        
//        if (advertisementMessage.isMy.integerValue) {
//
//        }else{
//            [self.redPacketState setTitle:[NSString stringWithFormat:@"%@人已领取,总计发放%@元",advertisementMessage.number,redMessage.money] forState:(UIControlStateNormal)];
//        }
        
        
        
    }
    
    WYUserMessage *userMessage = [WYStoreModule currentUserMessage];
    
    
    if (advertisementMessage.vipLevel.integerValue>userMessage.vipId.integerValue) {
        self.redPacketState.hidden = NO;
         [self.redPacketState setTitle:@"去开通会员领取该红包 >" forState:(UIControlStateNormal)];
        self.redPacketCount.text = @"您还不满足领取该红包的条件";
        self.redPacketCount.font = kFontSystemBIgger2(22);
        self.redPacketCount.textColor = [UIColor hexStringToColor:@"#aaaaaa" alpha:1.0];
    }
    
    if (advertisementMessage.vipLevel.integerValue==10) {
        self.followBtn.hidden = NO;
        self.followBtn.userInteractionEnabled = NO;
        [self.followBtn setImage:[UIImage imageNamed:@"isBaiYin"] forState:UIControlStateNormal];
    }
    if (advertisementMessage.vipLevel.integerValue==20) {
        self.followBtn.hidden = NO;
        self.followBtn.userInteractionEnabled = NO;
        [self.followBtn setImage:[UIImage imageNamed:@"isGold"] forState:UIControlStateNormal];
        
    }
    if (advertisementMessage.vipLevel.integerValue==30) {
        self.followBtn.hidden = NO;
        self.followBtn.userInteractionEnabled = NO;
        [self.followBtn setImage:[UIImage imageNamed:@"redPacketMessage_diamonds_exclusive"] forState:UIControlStateNormal];
        
    }
    
}










- (IBAction)taskAction:(id)sender {
    if (self.redMessage) {
        if (!_redMessage.isReceived.integerValue){
            if ([self.delegate respondsToSelector:@selector(didTaskButton)]) {
                [self.delegate didTaskButton];
            }
            
            
        }
    }
    if (self.advertisementMessage) {
        
        if (_advertisementMessage.vipLevel.integerValue) {
            if ([self.delegate respondsToSelector:@selector(gotoVip)]) {
                [self.delegate gotoVip];
            }
            
            
        }else{
            
            if (!_advertisementMessage.isReceived.integerValue){
                if ([self.delegate respondsToSelector:@selector(didTaskButton)]) {
                    [self.delegate didTaskButton];
                }
            }
        }
        
        
      
    }
    
    
}

- (IBAction)followAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(clickFollowActionWithType:)]) {
        [self.delegate clickFollowActionWithType:self.attentionState];
    }
}
-(void)setAttentionState:(NSString *)attentionState
{
    _attentionState = attentionState;
    self.followBtn.hidden = NO;
    
    if (self.redMessage.isMy.integerValue) {
        self.followBtn.hidden = YES;
    }
    
    
    if (attentionState.integerValue) {
        [self.followBtn setImage:[UIImage imageNamed:@"follow_button_button"] forState:(UIControlStateNormal)];
    }else{
       [self.followBtn setImage:[UIImage imageNamed:@"follow_button"] forState:(UIControlStateNormal)];
        
    }
    
    
}







@end
